<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\service\ServicePackageOrderService;
use apps\marriage\core\service\ServicePackageService;
use think\Exception;
use think\Request;

class ServicePackageOrderController extends BaseMarriageMpController
{
    /**
     * @var ServicePackageOrderService
     */
    private $servicePackageOrderService;

    /**
     * @var ServicePackageService
     */
    private $servicePackageService;

    protected function init()
    {
        parent::init();
        $this->servicePackageOrderService = service('ServicePackageOrder', SERVICE_NAMESPACE);
        $this->servicePackageService = service('ServicePackage', SERVICE_NAMESPACE);
    }

    public function create(Request $request)
    {
        $fromuid = $request->param('fromuid', 0, 'intval');
        $servicePackageId = $request->param('service_package_id', 0, 'intval');

        if($fromuid == 0) {
            $fromuid = $this->user['fromuid'];
        } else if($fromuid > 0 && $fromuid == $this->user['id']) {
            $fromuid = 0;
        }

        $package = $this->servicePackageService->getByPk($servicePackageId);
        if(empty($package) || $package['is_sale'] == 0) {
            throw new Exception('套餐不存在');
        }
        $orderMoney = floatval($package['price']);

        $order = [
            'user_id' => $this->user['id'],
            'fromuid' => $fromuid,
            'order_money' => $orderMoney,
            'service_package_id' => $servicePackageId
        ];
        $orderSn = $this->servicePackageOrderService->createOrder($order);
        return $this->success(['order_sn' => $orderSn]);
    }

    public function lists(Request $request)
    {
        $pageSize = $request->param('page_size', 10);
        $params = [
            'user_id' => $this->user['id'],
        ];
        $res = $this->servicePackageOrderService->pageListByParams($params, $pageSize);
        if($res['data']) {
            $dataList = [];
            foreach ($res['data'] as $rs) {
                $servicePackage = $this->servicePackageService->getByPk($rs['service_package_id']);
                $dataList[] = [
                    'package' => $servicePackage,
                    'order' => $this->servicePackageOrderService->format($rs)
                ];
            }
            $res['data'] = $dataList;
        }
        return $this->success($res);
    }

    public function detail(Request $request)
    {
        $orderSn = $request->param('order_sn', '');
        $order = $this->servicePackageOrderService->getByOrderSn($orderSn);
        if(empty($order) || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $servicePackage = $this->servicePackageService->getByPk($order['service_package_id']);
        return $this->success([
            'package' => $servicePackage,
            'order' => $order
        ]);
    }

    public function cancel(Request $request)
    {
        $orderSn = $request->param('order_sn', '');
        $order = $this->servicePackageOrderService->getByOrderSn($orderSn);
        if(empty($order) || $order['user_id'] != $this->user['id']) {
            throw new Exception('订单不存在');
        }
        $this->servicePackageOrderService->onCancel($order['id']);
        return $this->success();
    }
}